(ns pe.p16)

(defn dou [a]
  (loop [digits (next a) digit (first a) carry 0 result []]
    (cond
      (nil? digit)
        (cond (= carry 0) result
              :else (conj result 1))
      :else
        (let [sum (+ digit digit carry)]
          (cond
            (>= sum 10) (recur (next digits) (first digits) 1 (conj result (- sum 10)))
            :else (recur (next digits) (first digits) 0 (conj result sum)))))))


(defn pow [x to]
  (loop [acc 1 result [x]]
    (cond
      (= acc to) result
      :else (recur (inc acc) (dou result)))))

(println (reduce + (pow 2 1000)))